package com.cg.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cg.dao.UserInfoDao;
import com.cg.entity.BaseBusiness;
import com.cg.entity.FloorModel;
import com.cg.entity.GridAndBuildingVO;
import com.cg.entity.MapBuilding;
import com.cg.entity.UserInfo;
import com.cg.entity.UserInfoBuildingsVo;
import com.cg.utils.FilterRule;
import com.cg.utils.FilterRuleBuilder;
import com.cg.utils.R;
import com.cg.utils.model.MapBuildingsUtils;

/**
* @author LCW
* 
*/
@Service
public class UserInfoService  {
	@Autowired
	UserInfoDao userInfoDao;
	
	@Autowired
	MapBuildingService  mapBuildingService;
	
	@Autowired
	MapGridService mapGridService;
	
	
	
	public FloorModel queryUserByBuilding(Integer buildingId){
		List<FilterRule> filterRules = FilterRuleBuilder.newBuilder().key("building_id").eq().value(buildingId)
                .build();
		List<UserInfo> userInfos = userInfoDao.findByCondition(filterRules);
		Map<String, UserInfo> cacheUserInfoByAdress = cacheUserInfoByAdress(userInfos);
		
		MapBuilding mapBuilding = mapBuildingService.getById(buildingId);
		List<UserInfoBuildingsVo> calcFloorsByMapBuilding = MapBuildingsUtils.calcFloorsByMapBuilding(mapBuilding);
		
		FloorModel floorModel = new FloorModel();
		floorModel.setFloors(mapBuilding.getFloors());
		floorModel.setRooms(mapBuilding.getRooms());
		floorModel.setBuildingId(mapBuilding.getId());
		floorModel.setGridId(mapBuilding.getGridId());
		floorModel.setTotalRoom(mapBuilding.getFloors()*mapBuilding.getRooms());
		floorModel.setMapBuilding(mapBuilding);
		int totalNotMatch =0;
		for(UserInfoBuildingsVo vo : calcFloorsByMapBuilding) {
			vo.setGridId(mapBuilding.getGridId());
			if (null!=cacheUserInfoByAdress.get(vo.getBuildingAdress()))
				vo.setUsed(1);
			else
				totalNotMatch++;
		}		
		floorModel.setUserInfos(cacheUserInfoByAdress);
		floorModel.setNotMacth(totalNotMatch);
		floorModel.setLiveNumber(floorModel.getTotalRoom()-totalNotMatch);
		return floorModel;		
	}
	
	public List<UserInfo>  queryUserByAdress(Integer buildingId ,String adress){
		List<FilterRule> filterRules = FilterRuleBuilder.newBuilder().key("building_id").eq().value(buildingId)
				.and().key("building_address").eq().value(adress)
                .build();
		return userInfoDao.findByCondition(filterRules);
	}
	
	public GridAndBuildingVO queryUsersforMap(String userName) {
		List<FilterRule> filterRules = FilterRuleBuilder.newBuilder().key("user_name").eq().value(userName)
                .build();
		List<UserInfo> userInfos = userInfoDao.findByCondition(filterRules);
		if(null == userInfos || userInfos.isEmpty()) {
			return null;
		}
		Set<Integer> gridIds = new HashSet<Integer>();
		Set<Integer> buildingIds = new HashSet<Integer>();
		
		for(UserInfo user :userInfos) {
			if(user.getGridId() !=null) 
			  gridIds.add(user.getGridId());
			if(user.getBuildingId() !=null)
			  buildingIds.add(user.getBuildingId());	
		}
		GridAndBuildingVO vo =new GridAndBuildingVO();		
		vo.setGrids(mapGridService.getMapGridByIds(gridIds));
		vo.setBuiddings(mapBuildingService.getByIds(buildingIds));
		vo.setOuts(mapGridService.getPolygon());
		return vo;
		
	}
	
    private Map<String ,UserInfo> cacheUserInfoByAdress(List<UserInfo> userInfos){
      	Map<String ,UserInfo> chaces = new HashMap<String,UserInfo>();
      	if(null == userInfos)
      		 return chaces;
      	for(UserInfo user : userInfos) {
      		 if(StringUtils.isNotEmpty(user.getBuildingAddress())) 
      		  chaces.put(user.getBuildingAddress(), user);
      	}
      	return chaces;
    }
    
    
    public List<UserInfo> queryList(Map<String,Object> query){
		
		return userInfoDao.queryList(query);
	}
	
	
	
	public R save(UserInfo userinfo){
		userInfoDao.save(userinfo);
		return R.ok();
	}
	
	public int queryTotal(Map<String,Object> query) {
		
		return userInfoDao.queryTotal();
	}
	
	public R update(UserInfo userinfo){
		userInfoDao.update(userinfo);
		return R.ok();
	}
	
	public void deleteBatch(Long[] ids) {
		userInfoDao.deleteBatch(ids);
	}

	
	public UserInfo queryObject(Long id) {
		return userInfoDao.queryObject(id);
	}
	
	
	
	
	

}
